package com.wb3.organizer.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.wb3.organizer.model.Account;
import com.wb3.organizer.model.Note;
import com.wb3.organizer.service.NoteService;

public class NoteAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger log = Logger.getLogger(NoteAction.class);

	private Map<String, Object> session;

	private Note note;

	private List<Note> notes;

	private NoteService service;

	private String operate;

	private String msg;

	public void setSession(Map<String, Object> inSession) {
		this.session = inSession;
	}

	public Note getNote() {
		return note;
	}

	public void setNote(Note note) {
		this.note = note;
	}

	public List<Note> getNotes() {
		return notes;
	}

	public void setNotes(List<Note> notes) {
		this.notes = notes;
	}

	public NoteService getService() {
		return service;
	}

	public void setService(NoteService service) {
		this.service = service;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String create() {
		log.info("\n\n----------------------------------------------------------");
		log.debug("NoteAction.create()...\n");

		Account account = (Account) session.get("account");
		note.setAccount(account);
		note.setCreatedDT(new Date());

		service.saveNote(note);

		this.setOperate("noteCreateOK");
		this.setMsg("日记创建成功!!!");
		log.debug("NoteAction.list() Done\n");
		return SUCCESS;
	}

	public String update() {
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("NoteAction.update()...\n");

		Account account = (Account) session.get("account");
		note.setAccount(account);
		note.setCreatedDT(new Date());
		service.updateNote(note);

		this.setOperate("noteUpdateOK");
		this.setMsg("日记编辑成功!!!");
		log.debug("NoteAction.update() Done\n");
		return SUCCESS;
	}

	public String delete() {
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("NoteAction.delete()...\n");

		service.deleteNote(note);
		
		this.setOperate("noteDeleteOK");
		this.setMsg("日记删除成功!!!");
		log.debug("NoteAction.delete() Done\n");
		return SUCCESS;
	}

	public String list() {
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("NoteAction.list()...\n");

		Account account = (Account) session.get("account");
		notes = service.listNote(account);
		log.debug("NoteAction.list() Done\n");
		return SUCCESS;
	}

	public String forward() {
		if (operate == null) {
			return SUCCESS;
		} else {
			if (operate.equals("create")) {
				return "create";
			} else if (operate.equals("update")) {
				note = service.getNote(note.getNote_id());
				return "update";
			} else if (operate.equals("delete")) {
				return "delete";
			} else {
				return ERROR;
			}
		}
	}

}
